﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace NeuralNetwork.OutputTypes
{    
    abstract public class Output
    {       
        public Output(){}
        public Output(XmlNode node){}

        abstract public object Value();
        abstract public double ToDouble();
        abstract public XmlNode Save(XmlDocument doc, XmlNode parent);        
    }

    public class GenericOutput<T> : Output
    {
        private T value;
        public GenericOutput(){}
        public GenericOutput(T value) 
        { 
            this.value = value; 
        }
        
        public GenericOutput(XmlNode node)
        {
            value = (T)Convert.ChangeType(node.InnerText.Trim(), typeof(T));
        }

        public override XmlNode Save(XmlDocument doc, XmlNode parent)
        {
            parent.InnerText = value.ToString();
            return parent;
        }

        public override object Value()
        {
            return (object)value;
        }

        public override double ToDouble()
        {
            try
            {
                return (double)Convert.ChangeType(value, typeof(double));
            }
            catch 
            { 
                return 1.0;
            }
        }       

        public override string ToString()
        {
            return value.ToString();
        }
    }
}
